<?php
/**
 * Created by PhpStorm.
 * User: antely
 * Date: 2018/5/8
 * Time: 15:40
 * 主播产品 计划相关
 */
namespace  app\api\model;
use app\common\model\Base;
use think\Db;
class AnchorProduct extends  Base{
     protected $anchor ='anchor'; //主播表
     protected $product ='product'; //产品表
     protected $productContent='productContent'; //产品详细表
     protected $live ='live';//直播计划表
    protected  $liveProduct='liveProduct';//直播产品列表
    protected  $liveFollow='liveFollow';//主播关注表

    public function _initialize(){
        parent::_initialize();
    }

    //获取直接计划列表
    public function getLiveList($param){
        $where_new=' 1 ';
        $param['sort'] = !empty($param['sort'])?$param['sort']:'l.addtime';
        $param['order'] = !empty($param['order'])?$param['order']:'DESC';
        $param['page'] =!empty($param['page'])?$param['page']:'1';
        $param['rows'] =!empty($param['rows'])?$param['rows']:'20';
        $intkeys = ['is_change_price','status'];
        $strkeys = ['anchor_id','id','uid','current_room'];
        $likekeys = ['title'];
        $randkeys =[];
        $where_new.= \app\common\model\Base::buildSearch($intkeys, $strkeys, $randkeys, $likekeys,[],'l.');

        //根据主播名称查找计划列表
        if(!empty($param['anchor_name'])){
            $where_new.=' AND a.name like "%'.$param['anchor_name'].'%" ';
        }

        //默认不拿已删除的计划
        if(!isset($param['status'])){
            $where_new.=' AND l.status not in (-1)';
        }

        if(!empty($param['web_id'])){
            $where_new.=' AND l.web_id = '.$param['web_id'].'';
        }


        //时间区间查找 (开始时间 --结束时间)
        if(!empty($param['start_time']) && !empty($param['end_time'])){
            $where_new.=' AND l.start_time >= '.$param['start_time'].'  AND l.end_time <= '.$param['end_time'].' ';
        }else if(!empty($param['start_time'])){
            $where_new.=' AND l.start_time >= '.$param['start_time'].' ';
        }else if(!empty($param['end_time'])){
            $where_new.=' AND l.end_time <= '.$param['end_time'].'';
        }
        //直播间查找 (开始时间 --结束时间)
        if(!empty($param['live_start_time']) && !empty($param['live_end_time'])){
            $where_new.=' AND l.live_start_time >= '.$param['live_start_time'].'  AND l.live_end_time <= '.$param['live_end_time'].' ';
        }else if(!empty($param['live_start_time'])){
            $where_new.=' AND l.live_start_time >= '.$param['live_start_time'].' ';
        }else if(!empty($param['end_time'])){
            $where_new.=' AND l.live_end_time <= '.$param['live_end_time'].'';
        }
        $field='l.id,l.uid,l.title,l.start_time,l.end_time,l.anchor_id,l.live_start_time,l.live_end_time,l.is_change_price,l.current_room,l.current_product,l.status,l.price_range,l.addtime
       ,a.name,a.sex,a.id_front,a.id_back,a.mobile,a.open_id,a.qr_code,a.head_photo,a.	signature,a.	status as anchor_status,a.addtime as anchor_addtime,a.bandtime,a.weight,a.height,a.bust,a.waist,a.hip';
        $result['count'] =Db::connect(config('database.db_config1'))->name($this->live)->alias('l')->join('yali_anchor a','l.anchor_id =a.id')->where($where_new)->page($param['page'],$param['rows'])->order ($param ['sort'] . ' ' . $param ['order'] )->count();
        $result['list'] =Db::connect(config('database.db_config1'))->name($this->live)->alias('l')->join('yali_anchor a','l.anchor_id =a.id')->field($field)->where($where_new)->page($param['page'],$param['rows'])->order ($param ['sort'] . ' ' . $param ['order'] )->select();
        if(!empty($result['list'])){
            foreach ($result['list'] as $k =>$v){
                $result['list'][$k]['start_time']=!empty($v['start_time'])?date('Y-m-d H:i:s',$v['start_time']):0;
                $result['list'][$k]['end_time']=!empty($v['end_time'])?date('Y-m-d H:i:s',$v['end_time']):0;
                $result['list'][$k]['live_start_time']=!empty($v['live_start_time'])?date('Y-m-d H:i:s',$v['live_start_time']):0;
                $result['list'][$k]['live_end_time']=!empty($v['live_end_time'])?date('Y-m-d H:i:s',$v['live_end_time']):0;
                $result['list'][$k]['addtime']=!empty($v['addtime'])?date('Y-m-d H:i:s',$v['addtime']):0;
                $result['list'][$k]['bandtime']=!empty($v['bandtime'])?date('Y-m-d H:i:s',$v['bandtime']):0;
                $result['list'][$k]['anchor_addtime']=!empty($v['anchor_addtime'])?date('Y-m-d H:i:s',$v['anchor_addtime']):0;
                $result['list'][$k]['live_product_list']=Db::connect(config('database.db_config1'))->name($this->liveProduct)->alias('lp')->join('yali_product p','lp.product_id =p.id')->join('yali_product_content pc','lp.product_id =pc.product_id')->field('lp.id,lp.order_id,lp.live_id,lp.is_change_price,lp.product_id,p.unit_price,pc.pic,pc.market_price,pc.title')->where(['live_id'=>$v['id']])->order('lp.order_id asc')->select();
            }
        }
        return $result;
    }

    public function getAnchorList($param){
        $where_new=' 1 ';
        //默认不拿已删除的主播
        if(!isset($param['status'])){
            $where_new.=' AND status not in (-1)';
        }
        $param['sort'] = !empty($param['sort'])?$param['sort']:'addtime';
        $param['order'] = !empty($param['order'])?$param['order']:'DESC';
        $param['page'] =!empty($param['page'])?$param['page']:'1';
        $param['rows'] =!empty($param['rows'])?$param['rows']:'20';
        $intkeys = ['mobile','status'];
        $strkeys = ['open_id','id','uid'];
        $likekeys = ['name'];
        $randkeys =['addtime'];
        $where_new.= \app\common\model\Base::buildSearch($intkeys, $strkeys, $randkeys, $likekeys);
        $result['count'] =Db::connect(config('database.db_config1'))->name($this->anchor)->where($where_new)->page($param['page'],$param['rows'])->order ($param ['sort'] . ' ' . $param ['order'] )->count();
        $result['list'] =Db::connect(config('database.db_config1'))->name($this->anchor)->where($where_new)->page($param['page'],$param['rows'])->order ($param ['sort'] . ' ' . $param ['order'] )->select();
        if($result['list']){
            foreach ($result['list'] as $k =>$v){
                $result['list'][$k]['addtime']=!empty($v['addtime'])?date('Y-m-d H:i:s',$v['addtime']):null;
            }
        }
        return $result;
    }

    public function saveLive($param,$product_list=[]){
        if(!empty($param['id'])){
            $id =$param['id'];
            unset($param['id']);
            if(!empty($param)){
                if(!empty($param['start_time'])&&!empty($param['end_time'])){
                    if(time()>=$param['start_time']  && $param['end_time']>time()){
                        $param['status'] =1;
                    }
                }
                $result=Db::connect(config('database.db_config1'))->name($this->live)->where(['id'=>$id])->update($param);
                    if(empty($product_list)){
                        return  $result;
                    }
            }
            if(!empty($product_list) && $id){
               $rsf= Db::connect(config('database.db_config1'))->name($this->liveProduct)->where(['live_id'=>$id])->delete();
                if($rsf!==false){
                    $new_prodcut=[];
                    foreach ((array)$product_list as $k =>$v){
                        $new_prodcut[$k]['product_id']=$v;
                        $new_prodcut[$k]['live_id']=$id;
                        $new_prodcut[$k]['order_id']=$k+1;
                    }
                    unset($product_list);
                     if(Db::connect(config('database.db_config1'))->name($this->liveProduct)->insertAll($new_prodcut)!==false){
                         return $id;
                     }else{
                         return false;
                     }
                }else{
                    return false;
                }
            }
        }else{
            if(time()>=$param['start_time']  && $param['end_time']>time()){
                $param['status'] =1;
            }
            $insert=[
                'title'=>$param['title'],
                'anchor_id'=>$param['anchor_id'],
                'start_time'=>$param['start_time'],
                'end_time'=>$param['end_time'],
                'web_id'=>$param['web_id'],
                'price_range'=>!empty($param['price_range'])?$param['price_range']:0,
                'uid'=>!empty($param['uid'])?$param['uid']:0,
                'current_product'=>!empty($param['current_product'])?$param['current_product']:'',
                'live_end_time'=>!empty($param['live_end_time'])?$param['live_end_time']:'',
                'live_start_time'=>!empty($param['live_start_time'])?$param['live_start_time']:'',
                'status'=>!empty($param['status'])?$param['status']:0,
                'addtime'=>time()
            ];
            unset($param);
            $result=Db::connect(config('database.db_config1'))->name($this->live)->insertGetId($insert);
            if($result!==false){
                $new_prodcut=[];
                foreach ((array)$product_list as $k =>$v){
                    $new_prodcut[$k]['product_id']=$v;
                    $new_prodcut[$k]['live_id']=$result;
                    $new_prodcut[$k]['order_id']=$k+1;
                }
                unset($product_list);
               $reg = Db::connect(config('database.db_config1'))->name($this->liveProduct)->insertAll($new_prodcut);
                if($reg!==false){
                    return $result;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }

    public function deleteLive($param){
        $reg=Db::connect(config('database.db_config1'))->name($this->live)->where(['id'=>$param['id']])->update(['status'=>-1]);
        if($reg!==false){
            $result=Db::connect(config('database.db_config1'))->name($this->liveProduct)->where(['live_id'=>$param['id']])->delete();
           return  $result;
        }else{
            return false;
        }
    }

    public function liveFollowList($param){
        $where_new=' 1 ';
        $param['sort'] = !empty($param['sort'])?$param['sort']:'addtime';
        $param['order'] = !empty($param['order'])?$param['order']:'DESC';
        $param['page'] =!empty($param['page'])?$param['page']:'1';
        $param['rows'] =!empty($param['rows'])?$param['rows']:'20';
        $intkeys = ['anchor_id'];
        $strkeys = ['uid'];
        $likekeys = ['title'];
        $randkeys =['addtime'];
        $where_new.= \app\common\model\Base::buildSearch($intkeys, $strkeys, $randkeys, $likekeys,[],'l.');
        if(!empty($param['follow_username'])){
            $where_new.="And  (m.nickname like '%".$param['follow_username']."%'  or m.username like '%".$param['follow_username']."%') ";
        }
        if(!empty($param['web_id'])){
            $where_new.=' AND l.web_id = '.$param['web_id'].'';
        }
        $result['count']=Db::connect(config('database.db_config1'))->name($this->liveFollow)->alias('l')->join('yali_member m','l.uid =m.uid')->where($where_new)->page($param['page'],$param['rows'])->order ($param ['sort'] . ' ' . $param ['order'] )->count();
        $result['list']= Db::connect(config('database.db_config1'))->name($this->liveFollow)->alias('l')->join('yali_member m','l.uid =m.uid')->where($where_new)->field('l.uid,l.web_id,m.head_photo,m.nickname,m.username,l.addtime')->page($param['page'],$param['rows'])->order ($param ['sort'] . ' ' . $param ['order'] )->select();
        if(!empty($result['list'])){
           foreach ($result['list'] as $k=>$v){
               $result['list'][$k]['addtime']=$v['addtime']?date('Y-m-d H:i:s',$v['addtime']):0;
           }
       }
        return $result;
    }
}